﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MUD.Lib.Utils
{
    public class ANSI
    {
        public static string CSI = ASCIIEncoding.ASCII.GetString(new byte[] { 27 }) + "[{0}m";
        public enum Codes : byte
        {
            Reset = 0,
            Bold = 1,
            Italics = 3,
            Underline = 4,
            NoBold = 22,
            Black = 30,
            Red = 31,
            Green,
            Yellow,
            Blue,
            Magenta,
            Cyan,
            White,
            Default = 39,
            BlackBack,
            RedBack,
            GreenBack,
            YellowBack,
            BlueBack,
            MagentaBack,
            CyanBack,
            WhiteBack,
            DefaultBack = 49
        }

        public static string ColorCode(Codes code)
        {
            return String.Format(CSI, code.GetHashCode());
        }

        public static string ParseANSI(string input)
        {
            string[] split = input.Split(new string[] {"%^"}, StringSplitOptions.None);
            string[] ANSICodes = Enum.GetNames(Codes.Black.GetType());
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < split.Length; i++)
            {
                foreach (string code in ANSICodes)
                {
                    if (code.ToLower() == split[i].ToLower())
                    {
                        split[i] = ColorCode((Codes)Enum.Parse(Codes.Black.GetType(), code));
                    }
                }
                sb.Append(split[i]);
            }
            return sb.ToString();
        }

    }
}
